package com.pz.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.pz.gulimall.common.to.RegisterMemberTo;
import com.pz.gulimall.common.to.UserInfoTo;
import com.pz.gulimall.member.vo.MemberFocusSearchReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pz.gulimall.member.entity.MemberEntity;
import com.pz.gulimall.member.service.MemberService;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.R;

import javax.validation.Valid;


/**
 * 会员
 *
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-02 11:17:37
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 获取聊天对象ws注册的ID（实际获取的是注册的UserId）
     * @return 聊天对象在redis中注册的ID, ID没注册就返回 -1
     */
    @GetMapping("/get-channelId/{userId}")
    public R getChannelRegisterId(@PathVariable Long userId){
        return memberService.getChannelRegisterId(userId);
    }

    @PostMapping("/query-member-focus")
    public R getMemberAboutFocus(@RequestBody @Valid MemberFocusSearchReqVo vo){
        return memberService.queryMemberFocus(vo);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询某些字段的member
     * @param id    账号（username、mobile、email）
     * @return
     */
    @RequestMapping("/search")
    public R queryMember(@RequestParam String id){
        return memberService.queryMember(id);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        return memberService.queryUserInfoById(id);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 保存注册用户
     * @param member
     * @return
     */
    @RequestMapping("/save/register")
    public R saveRegister(@RequestBody @Valid RegisterMemberTo member){
        try {
            memberService.saveRegister(member);
        } catch (ExecutionException | InterruptedException e) {
            throw new RuntimeException(e);
        }
        return R.ok("添加member成功");
    }

    @PostMapping("/updateUserInfo")
    public R updateUserInfo(@RequestBody UserInfoTo userInfo){
        memberService.updateUserInfo(userInfo);
        return R.ok("修改成功！");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
